<template>
  <div class="home">
    <el-tabs
      v-model="activeName"
      type="border-card"
      @tab-click="handleClick"
      style="border: none"
    >
      <el-tab-pane label="用户管理" name="first" style="border: none">
        <template>
          <el-table :data="tableData" style="width: 100%">
            <el-table-column label="序号" width="220">
              <template slot-scope="scope">
                <span style="margin-left: 10px" class="indexaa"
                  >0{{ scope.$index + 1 }}</span
                >
              </template>
            </el-table-column>
            <el-table-column label="图片" width="220">
              <template slot-scope="scope">
                <img
                  :src="scope.row.pictureId"
                  alt=""
                  style="width: 60px; height: 60px"
                />
              </template>
              <!-- <img src="" alt=""> -->
            </el-table-column>
            <el-table-column label="合作伙伴名称" width="220">
              <template slot-scope="scope">
                <div>{{ scope.row.name }}</div>
              </template>
            </el-table-column>
            <el-table-column label="合作伙伴简介" width="750">
              <template slot-scope="scope">
                <div>{{ scope.row.content }}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEditzhid(scope.$index, scope.row)"
                  >置顶</el-button
                >
                <el-button
                  size="mini"
                  type="success"
                  @click="handleEdit(scope.$index, scope.row.id)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            background
            layout="prev, pager, next"
            :total="totalNum"
            @current-change="getcurr"
            @next-click="getnext"
            @prev-click="getprev"
          >
          </el-pagination>
        </template>
      </el-tab-pane>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="伙伴名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="伙伴内容" :label-width="formLabelWidth">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="queren">确 定</el-button>
        </div>
      </el-dialog>
      <el-tab-pane label="配置管理" name="second">
        <el-row style="height: 220px; margin-bottom: 20px">
          <el-col :span="1">
            <div
              class="grid-content bg-purple-light"
              style="height: 20px"
            ></div>
          </el-col>
          <el-col :span="6">
            <div class="grid-content bg-purple-light">
              <div
                class="tit"
                style="font-size: 24px; font-weight: bold; height: "
              >
                伙伴名称
              </div>
              <el-input
                placeholder="请输入内容"
                v-model="huoban_name"
                clearable
              >
              </el-input>
            </div>
          </el-col>
          <el-col :span="1" style="height: 20px"></el-col>
          <el-col :span="3">
            <div class="tit">状态</div>
            <div style="display: flex">
              <el-radio v-model="radio" label="1">备选项</el-radio>
              <el-radio v-model="radio" label="2">备选项</el-radio>
            </div>
          </el-col>
          <!-- <el-col :span="1" style="height: 20px"></el-col>
          <el-col :span="3">
            <div class="tit">时间</div>
            <el-time-select
              v-model="value"
              :picker-options="{
                start: '08:30',
                step: '00:15',
                end: '18:30',
              }"
              placeholder="选择时间" 
            >
            </el-time-select>
          </el-col> -->
          <el-col :span="1" style="height: 20px"></el-col>
          <el-col :span="3">
            <div class="tit">排序</div>
            <el-select v-model="value" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="1">
            <div
              class="grid-content bg-purple-light"
              style="height: 20px"
            ></div>
          </el-col>
          <el-col :span="3">
            <div class="grid-content bg-purple-light">
              <div class="tit">上传logo</div>
              <div>
                <el-upload
                  class="avatar-uploader"
                  action="http://123.56.105.243:8080/upload"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row
          :span="24"
          style="height: 2px; background-color: #dcdfe6"
        ></el-row>
        <el-row style="margin-top: 20px">
          <el-col :span="1" style="height: 20px"></el-col>
          <el-col :span="22"
            ><div style="text-align: left; font-weight: bold; font-size: 24px">
              编辑内容
            </div></el-col
          >
        </el-row>
        <el-row>
          <el-col :span="1" style="height: 20px"></el-col>
          <el-col :span="23">
            <el-input
              placeholder="请输入内容"
              v-model="huoban_content"
              clearable
              style="height: 200px"
              class="inp"
            >
            </el-input>
          </el-col>
        </el-row>
        <el-row style="margin-top: 20px">
          <el-col :span="24">
            <el-button type="info" plain>预览</el-button>
            <el-button
              type="primary"
              style="margin-left: 47px"
              @click="userSubnmit"
              >提交</el-button
            >
          </el-col>
        </el-row>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
// import { gethuoban } from "../request/api/getpagehb";
import { gethuoban } from "../../request/api/getpagehb";
import { delhuoban } from "../../request/api/delhuoban";
import { edithb } from "../../request/api/edithuoban";
import { addhuoban } from "../../request/api/addhuoban";
export default {
  data() {
    return {
      huoban_name: "",
      huoban_content: "",
      value: "", //选择时间
      radio: "1", //单选框
      imageUrl: "", //图片地址
      activeName: "first", //默认加载first内容
      // 上传图片的id
      imgId: 0,
      totalNum: 0,
      tableData: [
        // {
        //   date: "2016-05-02",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1518 弄",
        //   dic: "巴菲特在当地时间25日给伯克希尔哈撒韦公司(以下简称“伯克希尔”)的股东们写的一封信的开头。巴菲特这次将1600股伯克希尔A类股转换为240万股B类股，再将这些B类股分...",
        // },
        // {
        //   date: "2016-05-04",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1517 弄",
        //   dic: "巴菲特在当地时间25日给伯克希尔哈撒韦公司(以下简称“伯克希尔”)的股东们写的一封信的开头。巴菲特这次将1600股伯克希尔A类股转换为240万股B类股，再将这些B类股分...",
        // },
        // {
        //   date: "2016-05-01",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1519 弄",
        //   dic: "巴菲特在当地时间25日给伯克希尔哈撒韦公司(以下简称“伯克希尔”)的股东们写的一封信的开头。巴菲特这次将1600股伯克希尔A类股转换为240万股B类股，再将这些B类股分...",
        // },
        // {
        //   date: "2016-05-03",
        //   name: "王小虎",
        //   address: "上海市普陀区金沙江路 1516 弄",
        //   dic: "巴菲特在当地时间25日给伯克希尔哈撒韦公司(以下简称“伯克希尔”)的股东们写的一封信的开头。巴菲特这次将1600股伯克希尔A类股转换为240万股B类股，再将这些B类股分...",
        // },
      ],
      options: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
        {
          value: "3",
          label: "3",
        },
        {
          value: "4",
          label: "4",
        },
        {
          value: "5",
          label: "5",
        },
      ],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
        content: "",
      },
      formLabelWidth: "120px",
      editid: 0,
      pagesNum: 1,
    };
  },

  methods: {
    async handleEditzhid(e, ind) {
      console.log("1111", e, ind);
      let resres = this.tableData.splice(e, 1)[0];
      // console.log(":resres", resres);
      this.tableData.unshift(ind);
    },
    async userSubnmit() {
      let res = await addhuoban("/admin/partner", {
        content: this.huoban_content,
        id: 0,
        name: this.huoban_name,
        pictureId: this.imgId,
        sort: 0,
        status: 0,
        top: 0,
      });
      if (res.data.code == 200) {
        this.$message({
          type: "success",
          message: "数据提交成功",
        });
      } else {
        this.$message({
          type: "error",
          message: "该伙伴已存在,请重新添加",
        });
      }
      console.log("resres");
    },
    handleDelete(ind, ids) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await delhuoban(`/admin/partner/${ids}`);
          console.log("删除", res);

          // if (this.totalNum % 10 != 0) {
          //   console.log("dsadsad");
          //   this.getba(this.pagesNum - 1, 10);
          // } else {
          let restotal = this.totalNum - 1;
          if (restotal % 10 == 0) {
            console.log("dsadsaasdadsjkhdjkj");
            this.getba(this.pagesNum - 1, 10);
          } else {
            this.getba(this.pagesNum, 10);
            // console.log("页面", this.totalNum);
          }
          // }
          // if (this.tableData.length < 1) {
          // this.getba(this.pagesNum - 1, 10);
          // } else {
          // }
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async getaaa() {
      // let res=await
    },
    // async handleDelete(ind, ids) {
    //   let res = await delhuoban(`/admin/partner/${ids}`);
    //   console.log("删除", res);
    //   this.getba(1, 10);
    // },
    async handleEdit(ind, ids) {
      this.dialogFormVisible = true;
      this.editid = ids;
    },
    async queren() {
      this.dialogFormVisible = false;
      let res = await edithb("/admin/partner", {
        content: this.form.content,
        id: this.editid,
        name: this.form.name,
        pictureId: 0,
        sort: 0,
        status: 0,
        top: 0,
      });
      console.log("resresAAAA", res);
      if (res.data.code == 200) {
        this.$message({
          type: "success",
          message: "数据修改成功",
        });
      } else {
        this.$message({
          type: "error",
          message: "aa",
        });
      }
      this.getba(this.pagesNum, 10);
    },
    getnext(e) {
      console.log("res", e);
      this.pagesNum = e;
      this.getba(e, 10);
    },
    getcurr(e) {
      this.getba(e, 10);
    },
    getprev(e) {
      this.getba(e, 10);
    },
    async getba(page, pageSize) {
      let ress = await gethuoban(
        `/admin/partner/page?pageNum=${page}&pageSize=${pageSize}`
      );
      // console.log(111);
      console.log(ress);
      this.tableData = ress.data.rows;
      this.totalNum = ress.data.total;
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    handleAvatarSuccess(res, file) {
      console.log("resres", res.data[0].id);
      this.imgId = res.data[0].id;
      if (this.imgId) {
        this.$message({
          type: "success",
          message: "图片提交成功",
        });
      }
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.getba(this.pagesNum, 10);
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  text-align: right;
}
.tit {
  text-align: left;
  margin-left: 7px;
  margin-bottom: 10px;
  height: 31px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #ccc;
  background-color: #f6f8fb;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.indexaa {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #eff1f8;
  display: inline-block;
  text-align: center;
  line-height: 40px;
}

:deep .el-tabs--border-card > .el-tabs__header {
  background-color: #fff;
  border-bottom: none;
}

:deep .el-tabs--card > .el-tabs__header .el-tabs__nav {
  border: none;
}

:deep .el-tabs--card > .el-tabs__header {
  border: none;
}

:deep .el-tabs--card > .el-tabs__header .el-tabs__item {
  border: none !important;
}

:deep .el-tabs--border-card > .el-tabs__header .el-tabs__item:first-child {
  margin-right: 20px;
}

:deep .el-tabs--border-card > .el-tabs__header .el-tabs__item {
  border: none;
}

:deep .el-tabs--border-card > .el-tabs__header .el-tabs__item.is-active {
  background-color: #2567ff;
  color: #fff;
}

:deep
  .el-tabs--border-card
  > .el-tabs__header
  .el-tabs__item:not(.is-disabled):hover {
  color: #000;
}
:deep .el-input__prefix {
  display: none;
}
:deep .el-input--suffix .el-input__inner {
  background-color: #f6f8fb;
}

:deep .el-tabs__content {
  border-bottom: 2px solid #e8eaed;
}
:deep .inp input {
  height: 200px;
}
:deep .inp input::placeholder {
  transform: translateY(-80px);
}
:deep .el-main[data-v-7eb2bc79] {
  line-height: 0px !important;
}
:deep input[aria-hidden="true"] {
  display: none !important;
}
</style>